//
//  NSString+TimeFormat.m
//  eWeiXin
//
//  Created by chen wenhao on 14-3-31.
//  Copyright (c) 2014年 chen wenhao. All rights reserved.
//

#import "NSString+TimeFormat.h"

@implementation NSString (TimeFormat)
- (NSString *)prefixTime:(NSString *)timeStr
{
    NSString *prefix = nil;
    NSArray *array = [timeStr componentsSeparatedByString:@":"];
    NSInteger hour = [[array objectAtIndex:0] integerValue];
    if (hour >= 0 && hour < 6) {
        prefix = @"凌晨";
    }else if (hour >= 6 && hour < 12) {
        prefix = @"早上";
    }else if (hour >= 12 && hour < 14) {
        prefix = @"中午";
    }else if (hour >= 14 && hour < 18) {
        prefix = @"下午";
    }else if (hour >= 18 && hour < 24) {
        prefix = @"晚上";
    }
    
    return prefix;
}

- (NSString *)format
{
    NSDateFormatter *sourcDateFormatter = [[NSDateFormatter alloc] init];
    
    [sourcDateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *sourceDate = [sourcDateFormatter dateFromString:self];
    
    NSTimeInterval sourceTime = [sourceDate timeIntervalSince1970];
    
    
    NSDateFormatter* curDateFormatter = [[NSDateFormatter alloc]init];
    [curDateFormatter setDateFormat:@"yyyyMMdd"];
    NSString* date = [curDateFormatter stringFromDate:[NSDate date]];
    
    NSDate *todayZeroDate= [curDateFormatter dateFromString:date];
    
    
    NSTimeInterval todayZeroTime = [todayZeroDate timeIntervalSince1970];
    
    //时间和今天0点的时间差
    NSTimeInterval timeDifference = todayZeroTime-sourceTime;
    
    NSString *convertedStr = nil;
    
    NSDateFormatter* timeformatter = [[NSDateFormatter alloc]init];
    [timeformatter setDateFormat:@"HH:mm"];
    NSString* timeStr = [timeformatter stringFromDate:sourceDate];
    
    //说明是今天的时间
    if (timeDifference <= 0 && timeDifference >= -3600*24) {
        
        NSString* preTime = [self prefixTime:timeStr];
        
        convertedStr = [NSString stringWithFormat:@"%@%@",preTime,timeStr];
        
        //说明是昨天的
    }else if (timeDifference > 0 && timeDifference <= 3600*24) {
        
        NSString* preTime = [self prefixTime:timeStr];
        
        convertedStr = [NSString stringWithFormat:@"昨天%@%@",preTime,timeStr];
        //说明是前天以前的
    }else {
        
        NSDateFormatter* lastformatter = [[NSDateFormatter alloc]init];
        [lastformatter setDateFormat:@"MM月dd日"];
        
        NSString *date = [lastformatter stringFromDate:sourceDate];
        
        NSString* preTime = [self prefixTime:timeStr];
        
        convertedStr = [NSString stringWithFormat:@"%@%@%@",date,preTime,timeStr];
        
    }
    
    return convertedStr;
}

- (NSString *)formatTime
{
    NSDateFormatter *sourcDateFormatter = [[NSDateFormatter alloc] init];
    
    [sourcDateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *sourceDate = [sourcDateFormatter dateFromString:self];
    
    NSTimeInterval sourceTime = [sourceDate timeIntervalSince1970];
    
    
    NSDateFormatter* curDateFormatter = [[NSDateFormatter alloc]init];
    [curDateFormatter setDateFormat:@"yyyyMMdd"];
    
    NSDate *nowDate = [NSDate date];
    
    NSString* date = [curDateFormatter stringFromDate:nowDate];
    
    NSDate *todayZeroDate= [curDateFormatter dateFromString:date];
    
    
    NSTimeInterval todayZeroTime = [todayZeroDate timeIntervalSince1970];
    
    //时间和今天0点的时间差
    NSTimeInterval timeDifference = todayZeroTime-sourceTime;
    
    NSString *convertedStr = nil;
    
    NSDateFormatter* timeformatter = [[NSDateFormatter alloc]init];
    [timeformatter setDateFormat:@"HH:mm"];
    NSString* timeStr = [timeformatter stringFromDate:sourceDate];
    
    //说明是今天的时间
    if (timeDifference <= 0 && timeDifference >= -3600*24) {
        
        convertedStr = [NSString stringWithFormat:@"%@",timeStr];
        
        //说明是昨天的
    }else if (timeDifference > 0 && timeDifference <= 3600*24) {
        
        convertedStr = @"昨天";
        //说明是昨天以前
    }else {
    
        NSCalendar *cale = [NSCalendar currentCalendar];
        
        int unit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
        
        NSDateComponents *selfCmps = [cale components:unit fromDate:sourceDate];
        
        convertedStr = [NSString stringWithFormat:@"%ld-%ld",(long)selfCmps.month,(long)selfCmps.day];
        
    }

    return convertedStr;
}

- (NSString *)formatDate
{
    NSDateFormatter *sourcDateFormatter = [[NSDateFormatter alloc] init];
    
    [sourcDateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *sourceDate = [sourcDateFormatter dateFromString:self];
    
    NSDateFormatter* curDateFormatter = [[NSDateFormatter alloc]init];
    
    [curDateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSString* date = [curDateFormatter stringFromDate:sourceDate];
    
    return date;
}

- (NSString *)formatChineseDate
{
    NSDateFormatter *sourcDateFormatter = [[NSDateFormatter alloc] init];
    
    [sourcDateFormatter setDateFormat: @"yyyy-MM-dd"];
    
    NSDate *sourceDate = [sourcDateFormatter dateFromString:self];
    
    NSDateFormatter* curDateFormatter = [[NSDateFormatter alloc] init];
    
    [curDateFormatter setDateFormat:@"yyyy年MM月dd日"];
    
    NSString* date = [curDateFormatter stringFromDate:sourceDate];
    
    if (!date) {
        return self;
    }
    
    return date;
}

- (NSString *)convertChineseDateToNormal
{
    NSDateFormatter *sourcDateFormatter = [[NSDateFormatter alloc] init];
    
    [sourcDateFormatter setDateFormat:@"yyyy年MM月dd日"];
    
    NSDate *sourceDate = [sourcDateFormatter dateFromString:self];
    
    NSDateFormatter* curDateFormatter = [[NSDateFormatter alloc]init];
    
    [curDateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSString* date = [curDateFormatter stringFromDate:sourceDate];
    
    if (!date) {
        return self;
    }
    
    return date;
}

- (NSString *)formatTimeWithYYMMDD
{
    NSDateFormatter *sourcDateFormatter = [[NSDateFormatter alloc] init];
    
    [sourcDateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *sourceDate = [sourcDateFormatter dateFromString:self];
    
    NSDateFormatter* curDateFormatter = [[NSDateFormatter alloc]init];
    
    [curDateFormatter setDateFormat:@"yy-MM-dd"];
    
    NSString* date = [curDateFormatter stringFromDate:sourceDate];
    
    return date;
}

- (BOOL)isOver48Hour
{
    NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
    
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date1=[formatter dateFromString:self];
    
    NSTimeInterval date=[[NSDate date] timeIntervalSinceDate:date1];
    
    if(date<=48*3600)
    {
        return NO;
    }
    else
    {
        return YES;
    }
}

- (NSString *)splitNumber
{
    NSMutableString *str = [NSMutableString string];
    
    NSInteger length = self.length;
    
    NSInteger count = length/4;
    
    NSInteger remain = length%4;
    
    for (int i=0; i<count; i++) {
        
        if (count*4>=length) {
            NSString *s = [self substringWithRange:NSMakeRange(i*4, 4)];
            [str appendString:s];
            
            if ((i+1)*4<length) {
                [str appendString:@" "];
            }
        }
        
    }
    
    if (remain>0) {
        [str appendString:@" "];
        NSString *s = [self substringWithRange:NSMakeRange(count*4-1, remain)];
        [str appendString:s];
    }
    
    return str;
    
}
+(NSString *)getSystemDate
{
    NSDate *senddate = [NSDate date];
    
    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
    
    [dateformatter setDateFormat:@"YYYYMMdd"];
    
    NSString *  locationString=[dateformatter stringFromDate:senddate];
    return locationString;
}

@end
